package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.pojo.StatusUpdate;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.server.CategoryServer;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryServer categoryServer;

    /**
     * 按类型查询分类
     */
    @GetMapping("/list")
    public Result<List<Category>> listByType(@RequestParam Integer type) {
        List<Category> categoryList = categoryServer.listByType(type);
        return Result.success(categoryList);
    }


    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> pageEmployee(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryServer.pageEmployee(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 修改分类状态
     */
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status, Long id) {
        StatusUpdate statusUpdate = new StatusUpdate(status, id);
        categoryServer.status(statusUpdate);
        return Result.success();
    }

    @PutMapping
    public Result<String> updateById(@RequestBody CategoryDTO categoryDTO){
        categoryServer.update(categoryDTO);
        return Result.success("修改成功");
    }


    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        categoryServer.addCategory(categoryDTO);
        return Result.success("添加成功");
    }


    /**
     * 删除分类
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result<String> deleteById(Long id){
        log.info("删除分类：{}", id);
        categoryServer.deleteById(id);
        return Result.success();
    }

}
